package org.lib.jpamysql.query;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * 修改数据
 * @author ben
 * @date 2021-10-31 11:18:58 CST
 */
@Repository(value = "user4Dao")
//@Transactional // 也可以，接口或方法上二选一添加，否则 更新异常
public interface User4DAO extends JpaRepository<User, Long> {

	@Transactional // 必须添加，否则异常 TransactionRequiredException
	@Modifying
	@Query(value="UPDATE User u SET u.age = :age WHERE u.id = :id")
	Integer updateAge(@Param(value = "id") Long id, @Param("age") Integer newAge);
	
	@Transactional // 必须添加，否则异常 TransactionRequiredException
	@Modifying
	@Query(value="DELETE FROM User u WHERE u.id = :id")
	Integer deleteUser(@Param(value = "id") Long id);
	
}
